const arena = {
	namespaced: true,
    state:{
        weekStr: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
		// 已选择的场次
		selectedArean: [],
		// 不能选择的裁判（在已选择的场次时段里，有裁判已被其他场地的重合时段预订）, 场次作为键，场次对应的已预订裁判列表作为值
		bookedReferees: new Map(),
		// 当 bookedReferees 发生变化时，改变该数据来触发 bookedRefereesArr 的响应式更新
		bookedRefereesChanged: 0
    },
	
	getters: {
		// 通过场次所在半场时段列表中的索引，来查找它是否存在于当前已选择列表中
		findSelectedAreanByIdx(state, getters) {
			return (index, part) => state.selectedArean.findIndex(arena => arena.index === index && arena.part === part)
		},
		
		bookedRefereesArr(state) {
			// 通过获取 bookedRefereesChanged 来声明依赖这个数据，从而获取 bookedReferees 的更新
			const changed = state.bookedRefereesChanged
			return [...state.bookedReferees.values()]
		}
	},
	
	mutations: {
		// 如果该时段不存在于 selectedArean 里则添加，已存在则删除
		selectArena(state, payload) {
			const index = state.selectedArean.findIndex(arena => payload.name == arena.name && payload.part == arena.part && payload.section.time_fragment_id == arena.section.time_fragment_id)
			if(index === -1) {
				state.selectedArean.push(payload)	
			}else{
				state.selectedArean.splice(index, 1)
			}
		},
		
		// 删除已存在的时段，index 为该时段在已选择时段列表里的索引
		deleteArena(state, index) {
			// 若该时段所在半场有连续限制，则删除属于该半场的所有已选择的时段
			if(state.selectedArean[index].continuity > 0) {
				const part = state.selectedArean[index].part
				// 复制一个具有相同顺序已选择时段列表（只保留用到的数据）
				const selectedIndexArr = state.selectedArean.map(item => {
					return {
						index: item.index,
						part: item.part
					}
				})
				// 以复制的已选择时段列表为循环参照
				selectedIndexArr.forEach((arena, idx) => {
					if(arena.part === part) {
						
						// 目前只保存同一号场同一天的场次，所以只需判断半场和时段索引就可以了
						const delIdx = state.selectedArean.findIndex(item => item.part === arena.part && item.index === arena.index)
						const deleted = state.selectedArean.splice(delIdx, 1)
						// 移除对应的禁选裁判列表
						this.commit('arena/removeBookedReferees', {arena: deleted[0]})
						
					}
				})
			}else{
				const deleted = state.selectedArean.splice(index, 1)
				// 移除对应的禁选裁判列表
				this.commit('arena/removeBookedReferees', {arena: deleted[0]})
			}
		},
		
		// 添加连续时段到已选择时段列表，若已存在某个时段则跳过该时段
		addArenaContinuously(state, payload) {
			const index = state.selectedArean.findIndex(arena => payload.name == arena.name && payload.part == arena.part && payload.section.time_fragment_id == arena.section.time_fragment_id)
			if(index === -1) {
				state.selectedArean.push(payload)
			}
		},
		
		// 清空已选择时段列表
		clearSelectedArena(state) {
			state.selectedArean = []
		},
		
		// 把某个场次（时段）的已预订裁判添加进 bookedReferees
		addBookedReferees(state, { arena, referees }) {
			// 判断该场次是否已存在集合中
			if(state.bookedReferees.size > 0) {
				for(let key of state.bookedReferees.keys()) {
					const existed = key.name == arena.name && key.part == arena.part && key.section.time_fragment_id == arena.section.time_fragment_id
					if(!existed) {
						state.bookedReferees.set(arena, referees)
					}
				}
			}else{
				state.bookedReferees.set(arena, referees)
			}
			
			
			console.log(state.bookedReferees)
			state.bookedRefereesChanged += 1
		},
		
		// 当取消选择某个场次（时段）的时候，把该时段的禁选裁判从 bookedReferees 中移除
		removeBookedReferees(state, { arena }) {
			for(let key of state.bookedReferees.keys()) {
				if(key.name == arena.name && key.part == arena.part && key.section.time_fragment_id == arena.section.time_fragment_id) {
					state.bookedReferees.delete(key)
				}
			}
			console.log(state.bookedReferees)
			state.bookedRefereesChanged += 1
		},
		
		// 清空禁选的裁判列表（切换日期和支付成功后会使用）
		clearBookedReferees(state) {
			state.bookedReferees = new Map()
			state.bookedRefereesChanged += 1
		}
	}
}
export default arena